/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class TypejoJpaController implements Serializable {

    public TypejoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public TypejoJpaController() {
    }
    
    
    
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Typejo typejo) {
        if (typejo.getJournauxCollection() == null) {
            typejo.setJournauxCollection(new ArrayList<Journaux>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Journaux> attachedJournauxCollection = new ArrayList<Journaux>();
            for (Journaux journauxCollectionJournauxToAttach : typejo.getJournauxCollection()) {
                journauxCollectionJournauxToAttach = em.getReference(journauxCollectionJournauxToAttach.getClass(), journauxCollectionJournauxToAttach.getNumjo());
                attachedJournauxCollection.add(journauxCollectionJournauxToAttach);
            }
            typejo.setJournauxCollection(attachedJournauxCollection);
            em.persist(typejo);
            for (Journaux journauxCollectionJournaux : typejo.getJournauxCollection()) {
                Typejo oldTypejoOfJournauxCollectionJournaux = journauxCollectionJournaux.getTypejo();
                journauxCollectionJournaux.setTypejo(typejo);
                journauxCollectionJournaux = em.merge(journauxCollectionJournaux);
                if (oldTypejoOfJournauxCollectionJournaux != null) {
                    oldTypejoOfJournauxCollectionJournaux.getJournauxCollection().remove(journauxCollectionJournaux);
                    oldTypejoOfJournauxCollectionJournaux = em.merge(oldTypejoOfJournauxCollectionJournaux);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Typejo typejo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typejo persistentTypejo = em.find(Typejo.class, typejo.getIdTypejo());
            Collection<Journaux> journauxCollectionOld = persistentTypejo.getJournauxCollection();
            Collection<Journaux> journauxCollectionNew = typejo.getJournauxCollection();
            List<String> illegalOrphanMessages = null;
            for (Journaux journauxCollectionOldJournaux : journauxCollectionOld) {
                if (!journauxCollectionNew.contains(journauxCollectionOldJournaux)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Journaux " + journauxCollectionOldJournaux + " since its typejo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Journaux> attachedJournauxCollectionNew = new ArrayList<Journaux>();
            for (Journaux journauxCollectionNewJournauxToAttach : journauxCollectionNew) {
                journauxCollectionNewJournauxToAttach = em.getReference(journauxCollectionNewJournauxToAttach.getClass(), journauxCollectionNewJournauxToAttach.getNumjo());
                attachedJournauxCollectionNew.add(journauxCollectionNewJournauxToAttach);
            }
            journauxCollectionNew = attachedJournauxCollectionNew;
            typejo.setJournauxCollection(journauxCollectionNew);
            typejo = em.merge(typejo);
            for (Journaux journauxCollectionNewJournaux : journauxCollectionNew) {
                if (!journauxCollectionOld.contains(journauxCollectionNewJournaux)) {
                    Typejo oldTypejoOfJournauxCollectionNewJournaux = journauxCollectionNewJournaux.getTypejo();
                    journauxCollectionNewJournaux.setTypejo(typejo);
                    journauxCollectionNewJournaux = em.merge(journauxCollectionNewJournaux);
                    if (oldTypejoOfJournauxCollectionNewJournaux != null && !oldTypejoOfJournauxCollectionNewJournaux.equals(typejo)) {
                        oldTypejoOfJournauxCollectionNewJournaux.getJournauxCollection().remove(journauxCollectionNewJournaux);
                        oldTypejoOfJournauxCollectionNewJournaux = em.merge(oldTypejoOfJournauxCollectionNewJournaux);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = typejo.getIdTypejo();
                if (findTypejo(id) == null) {
                    throw new NonexistentEntityException("The typejo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typejo typejo;
            try {
                typejo = em.getReference(Typejo.class, id);
                typejo.getIdTypejo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The typejo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Journaux> journauxCollectionOrphanCheck = typejo.getJournauxCollection();
            for (Journaux journauxCollectionOrphanCheckJournaux : journauxCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Typejo (" + typejo + ") cannot be destroyed since the Journaux " + journauxCollectionOrphanCheckJournaux + " in its journauxCollection field has a non-nullable typejo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(typejo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Typejo> findTypejoEntities() {
        return findTypejoEntities(true, -1, -1);
    }

    public List<Typejo> findTypejoEntities(int maxResults, int firstResult) {
        return findTypejoEntities(false, maxResults, firstResult);
    }

    private List<Typejo> findTypejoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Typejo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Typejo findTypejo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Typejo.class, id);
        } finally {
            em.close();
        }
    }

    public int getTypejoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Typejo> rt = cq.from(Typejo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
